package com.caiden_micheal.dynamic.record.service.impl;

import com.caiden_micheal.base.domain.AjxResult;
import com.caiden_micheal.dynamic.form_change.mapper.FormChangeBaseDataMapper;
import com.caiden_micheal.dynamic.process.domain.DynamicProcess;
import com.caiden_micheal.dynamic.process.mapper.DProcessMapper;
import com.caiden_micheal.dynamic.record.domain.DProcessRecord;
import com.caiden_micheal.dynamic.record.dto.DProcessRecordDTO;
import com.caiden_micheal.dynamic.record.dto.FindProcessRecordDTO;
import com.caiden_micheal.dynamic.record.mapper.DProcessRecordMapper;
import com.caiden_micheal.dynamic.record.service.DProcessRecordService;
import com.caiden_micheal.user.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DProcessRecordServiceImpl implements DProcessRecordService {
    private static final Logger logger = LoggerFactory.getLogger(DProcessRecordServiceImpl.class);

    @Autowired
    private DProcessRecordMapper dProcessRecordMapper;
    @Autowired
    private FormChangeBaseDataMapper formChangeBaseDataMapper;
    @Autowired
    private DProcessMapper dProcessMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public AjxResult findProcessRecordById(FindProcessRecordDTO findProcessRecordDTO) {
        if (findProcessRecordDTO == null || findProcessRecordDTO.getProcessId() == null) {
            return AjxResult.paramError("参数传递有误");
        }
        try {
            DynamicProcess dynamicProcess = dProcessMapper.findById(findProcessRecordDTO.getProcessId());
            if (dynamicProcess == null) {
                return AjxResult.paramError("流程不存在");
            }
            List<DProcessRecord> dProcessRecordList = dProcessRecordMapper.findByProcessIdHasPerson(findProcessRecordDTO.getProcessId());
            if (dProcessRecordList == null || dProcessRecordList.size() == 0) {
                return AjxResult.processError("流程未启动或被人为删库");
            }
            List<DProcessRecordDTO> dProcessRecordDTOList = new ArrayList<>();
            List<String> usernameList = new ArrayList<>();
            List<Long> dProcessRecordIdList = new ArrayList<>();
            for (DProcessRecord dProcessRecord : dProcessRecordList) {
                DProcessRecordDTO dProcessRecordDTO = new DProcessRecordDTO();
                BeanUtils.copyProperties(dProcessRecord, dProcessRecordDTO);
                dProcessRecordDTOList.add(dProcessRecordDTO);
                usernameList.add(dProcessRecordDTO.getProcessorUsername());
                dProcessRecordIdList.add(dProcessRecordDTO.getId());
            }
            Map<String, Map<String, String>> userResultMap = userMapper.findMapByUsernameList(usernameList);
            for (DProcessRecordDTO dProcessRecordDTO : dProcessRecordDTOList) {
                Map<String, String> userTempMap = userResultMap.get(dProcessRecordDTO.getProcessorUsername());
                if (userTempMap != null) {
                    dProcessRecordDTO.setProcessorName(userTempMap.get("name"));
                    dProcessRecordDTO.setProcessorOrgName(userTempMap.get("org_name"));
                }
                dProcessRecordDTO.setFormChangeRecordList(formChangeBaseDataMapper.findByRecordId(dProcessRecordDTO.getId()));
            }
            return AjxResult.success(dProcessRecordDTOList);
        } catch (Exception e) {
            logger.error(e.toString());
            return AjxResult.unknownError("数据库操作异常");
        }
    }
}
